import 'package:flutter/material.dart';
import 'package:flutter_danmu_anim/common/skeleton/video_card_h.dart';
import 'package:flutter_danmu_anim/common/widgets/http_error.dart';
import 'package:flutter_danmu_anim/common/widgets/video_card_h.dart';
import 'package:flutter_danmu_anim/model/model_hot_video_item.dart';
import 'package:flutter_danmu_anim/page/video/detail/related/controller.dart';
import 'package:get/get.dart';

class RelatedVideoPanel extends StatefulWidget {
  const RelatedVideoPanel({super.key});

  @override
  State<RelatedVideoPanel> createState() => _RelatedVideoPanelState();
}

class _RelatedVideoPanelState extends State<RelatedVideoPanel> {
  final RelatedController _relatedController =
  Get.put(RelatedController(), tag: Get.arguments['heroTag']);

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _relatedController.queryRelatedVideo(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          var status = snapshot.data?.status ?? false;
          List<HotVideoItemModel> videoList = snapshot.data?.data ?? [];
          if (status) {
            return SliverList(
                delegate: SliverChildBuilderDelegate((context, index) {
              if (index == videoList.length) {
                return SizedBox(height: MediaQuery.of(context).padding.bottom);
              } else {
                return VideoCardH(
                  videoItem: videoList[index],
                );
              }
            }, childCount: videoList.length + 1));
          } else {
            return HttpError(
                errMsg: snapshot.data?.msg ?? "", fn: () => setState(() {}));
          }
        } else {
          // 骨架屏
          return SliverList(
            delegate: SliverChildBuilderDelegate((context, index) {
              return const VideoCardHSkeleton();
            }, childCount: 5),
          );
        }
      },
    );
  }
}
